package com.wjk.kylin.mall.product.controller;

import com.wjk.kylin.mall.common.core.dto.base.BaseOutDTO;
import com.wjk.kylin.mall.common.core.dto.base.DataIdInDTO;
import com.wjk.kylin.mall.product.dto.ProductInDTO;
import com.wjk.kylin.mall.product.service.IProductService;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;

/**
 * @Author: wangjinkui
 * @Date: 2021/5/28
 * @Description : 商品
 */
@Slf4j
@RestController
@RequestMapping("/product")
public class ProductController {

    //    @Autowired
//    private RedissonClient redissonClient;
    @Autowired
    private IProductService productService;
    @Autowired
    private MeterRegistry registry;
    private Counter productAddErrorCounter;

    @PostConstruct
    private void init() {
        productAddErrorCounter = registry.counter("mall_product_add_exception_total");
    }

    /**
     * 查询
     */
    @PostMapping("/get")
    public BaseOutDTO get(@Validated @RequestBody DataIdInDTO inDTO) {
        return productService.getById(inDTO);
    }

    @PostMapping("/add")
    public BaseOutDTO add(@Validated @RequestBody ProductInDTO inDTO) {
        try {
            return productService.add(inDTO);
        } catch (Exception e) {
            //TODO 统计商品新增失败次数
            productAddErrorCounter.increment();
            throw e;
        }
    }

//    /**
//     * 新增
//     */
//    @PostMapping("/add")
//    public BaseOutDTO add(@Validated @RequestBody ProductInDTO inDTO) {
//        //分布式锁
//        String lockKey = "kylin:mall:product:add:" + inDTO.getProductCode();
//        log.info("thread:{},lockKey:{}", Thread.currentThread().getName(), lockKey);
//
//        RLock lock = redissonClient.getLock(lockKey);
//        BaseOutDTO outDTO = BaseOutDTO.error();
//        log.info("thread:{},lockKey:{},lock:{}", Thread.currentThread().getName(), lockKey, lock);
//        try {
//            boolean isLock = lock.tryLock(1, TimeUnit.MILLISECONDS);
//            log.info("thread:{},lockKey:{},isLock:{},lock:{}", Thread.currentThread().getName(), lockKey, isLock, lock);
//            if (isLock) {
//                //业务处理
//                outDTO = productService.add(inDTO);
//            } else {
//                outDTO.setMsg("正在处理中，请稍后尝试！");
//                log.error("thread:{},lockKey:{},isLock:{},lock:{},msg:{}", Thread.currentThread().getName(), lockKey, false, lock, "排队中,请稍后重试!");
//            }
//        } catch (InterruptedException e) {
//            log.error("e:", e);
//            outDTO.setMsg("正在处理中，请稍后尝试！");
//        } catch (Exception e) {
//            //TODO 统计商品新增失败次数
//            productAddErrorCounter.increment();
//            throw e;
//        } finally {
//            boolean locked = lock.isLocked();
//            log.info("thread:{},lockKey:{},locked:{},lock:{}", Thread.currentThread().getName(), lockKey, locked, lock);
//            if (lock.isHeldByCurrentThread() && locked) {
//                lock.unlock();
//            }
//        }
//
//        return outDTO;
//    }
}
